package guilayer;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import modellayer.CompanyType;
import modellayer.KeyPerson;
import modellayer.Service;
import modellayer.ZipCode;
import controllerlayer.AppointmentCtrl;
import controllerlayer.CompanyCtrl;
import controllerlayer.TimestampCtrl;
import controllerlayer.ZipCodeCtrl;

public class CreateCompany extends JFrame
{	
	private JPanel contentPane;
	
	private TimestampCtrl tc = new TimestampCtrl();	
	private ZipCodeCtrl zcc = new ZipCodeCtrl();
	private CompanyCtrl cc = new CompanyCtrl();
	private JLabel lblCvr, lblPhone, lblName, lblType, lblAddress, lblZipCodeCity, lblError, lblEmail, lblHjemmeside, lblHovedService, lblAndenService, lblAntalAnsatte;
	private JTextField txtCvr, txtName, txtPhone, txtAddress, txtZipCode, txtCity, txtMail, txtWebside, txtNoOfEmp;
	private JComboBox cbMainService, cbSecService, cbType;
	
	/**
	 * Create the frame.
	 */	
	public CreateCompany() 
	{
		/**
		 * Setup frame
		 */
		setTitle("Fairway Networks | Opret Aftale");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 475, 524);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		getContentPane().setLayout(null);
		contentPane.setLayout(null);
		
		
		/**
		 * Set up JLabels
		 */		
		lblCvr = new JLabel("Cvr nummer:");
		lblCvr.setBounds(10, 11, 124, 14);
		getContentPane().add(lblCvr);
		
		lblPhone = new JLabel("Hoved tlf:");
		lblPhone.setBounds(10, 166, 124, 14);
		getContentPane().add(lblPhone);
		
		lblName = new JLabel("Firma navn:");
		lblName.setBounds(10, 42, 124, 14);
		getContentPane().add(lblName);
		
		lblType = new JLabel("Firma type:");
		lblType.setBounds(10, 73, 124, 14);
		contentPane.add(lblType);
		
		lblAddress = new JLabel("Adresse:");
		lblAddress.setBounds(10, 104, 124, 14);
		getContentPane().add(lblAddress);
		
		lblZipCodeCity = new JLabel("Postnummer/By:");
		lblZipCodeCity.setBounds(10, 135, 124, 14);
		getContentPane().add(lblZipCodeCity);
		
		lblEmail = new JLabel("E-mail:");
		lblEmail.setBounds(10, 197, 124, 14);
		getContentPane().add(lblEmail);
		
		lblHjemmeside = new JLabel("Hjemmeside:");
		lblHjemmeside.setBounds(10, 228, 124, 14);
		getContentPane().add(lblHjemmeside);
		
		lblHovedService = new JLabel("Hoved service");
		lblHovedService.setBounds(10, 259, 124, 14);
		getContentPane().add(lblHovedService);
		
		lblAndenService = new JLabel("Anden service");
		lblAndenService.setBounds(10, 290, 124, 14);
		getContentPane().add(lblAndenService);
		
		lblAntalAnsatte = new JLabel("Antal ansatte");
		lblAntalAnsatte.setBounds(10, 321, 124, 14);
		getContentPane().add(lblAntalAnsatte);
		
		lblError = new JLabel("");
		lblError.setForeground(Color.RED);
		lblError.setBounds(10, 461, 214, 14);
		contentPane.add(lblError);
		
		/**
		 * Setup input areas
		 */	
		txtCvr = new JTextField();
		txtCvr.setColumns(10);
		txtCvr.setBounds(144, 11, 305, 20);
		contentPane.add(txtCvr);
		
		txtName = new JTextField();
		txtName.setColumns(10);
		txtName.setBounds(144, 42, 305, 20);
		contentPane.add(txtName);
		
		cbType = new JComboBox(listOfTypes());
		cbType.setEditable(false);
		cbType.setBounds(144, 73, 305, 20);
		contentPane.add(cbType);
		
		txtAddress = new JTextField();
		txtAddress.setBounds(144, 104, 305, 20);
		getContentPane().add(txtAddress);
		txtAddress.setColumns(10);
		
		txtZipCode = new JTextField();
		txtZipCode.addKeyListener(new KeyListener() {
			
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				setCity();
				checkLength();
			}
			
			public void keyPressed(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		txtZipCode.setBounds(144, 135, 80, 20);
		getContentPane().add(txtZipCode);
		txtZipCode.setColumns(10);	
		
		txtCity = new JTextField();
		txtCity.setEditable(false);
		txtCity.setBounds(234, 135, 215, 20);
		contentPane.add(txtCity);
		txtCity.setColumns(10);
		
		txtPhone = new JTextField();
		txtPhone.setColumns(10);
		txtPhone.setBounds(144, 166, 305, 20);
		contentPane.add(txtPhone);
		
		txtMail = new JTextField();
		txtMail.setColumns(10);
		txtMail.setBounds(144, 197, 305, 20);
		contentPane.add(txtMail);
		
		txtWebside = new JTextField();
		txtWebside.setColumns(10);
		txtWebside.setBounds(144, 228, 305, 20);
		contentPane.add(txtWebside);
		
		cbMainService = new JComboBox(listOfServices());
		cbMainService.setBounds(144, 259, 305, 20);
		contentPane.add(cbMainService);
		
		cbSecService = new JComboBox(listOfServices());
		cbSecService.setBounds(144, 290, 305, 20);
		contentPane.add(cbSecService);
		
		txtNoOfEmp = new JTextField();
		txtNoOfEmp.setColumns(10);
		txtNoOfEmp.setBounds(144, 321, 305, 20);
		contentPane.add(txtNoOfEmp);
		
		/**
		 * Setup buttons
		 */		
		JButton btnSave = new JButton("Gem");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (createCompany()) 
					closeWindow();
			}
		});
		btnSave.setBounds(261, 452, 89, 23);
		contentPane.add(btnSave);
		
		JButton btnCancel = new JButton("Annuller");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeWindow();
			}
		});
		btnCancel.setBounds(360, 452, 89, 23);
		contentPane.add(btnCancel);	
	}
	
	private void setCity()
	{
		try {
			if( zcc.findZipCode(Integer.parseInt(txtZipCode.getText())) != null)
			{
				int zipcode = Integer.parseInt(txtZipCode.getText());
				ZipCode zc = zcc.findZipCode(zipcode);
				
				txtCity.setText(zc.getCity());
			}	
		} catch (NumberFormatException e) {
			setError("Kun tal i postnr. feltet!");
		}
	}
	
	private void checkLength()
	{
		String string = txtZipCode.getText();
		if (string.length()>4) {
			txtZipCode.setText(string.substring(0, 4));
		}
	}

	private void closeWindow() 
	{
		dispose();
	}	
	
	private Object[] listOfTypes()
	{
		ArrayList<CompanyType> tList = new ArrayList<CompanyType>();
		tList = cc.listOfAllTypes();
		
		return tList.toArray();
	}
	
	private Object[] listOfServices()
	{
		ArrayList<Service> sList = new ArrayList<Service>();
		sList = cc.listOfAllServices();
		
		return sList.toArray();
	}
	
	private boolean createCompany()
	{
		boolean success = false;
		AppointmentCtrl ac = new AppointmentCtrl();
		
		int cvr;
		String name;
		CompanyType companyType;
		String address;
		int zc;
		ZipCode zipCode;
		String mainPhone;
		String mainMail;
		String website;
		Service mainService;
		Service secService;
		int noOfEmployees;
		long addedDate;
		
		try
		{
			cvr = Integer.parseInt(txtCvr.getText());		
			name = txtName.getText();
			companyType = (CompanyType) cbType.getSelectedItem();
			address = txtAddress.getText();
			zc = Integer.parseInt(txtZipCode.getText());
			zipCode = zcc.findZipCode(zc);
			mainPhone = txtPhone.getText();
			mainMail = txtMail.getText();
			website = txtWebside.getText();
			mainService = (Service) cbMainService.getSelectedItem();
			secService = (Service) cbSecService.getSelectedItem();
			noOfEmployees = Integer.parseInt(txtNoOfEmp.getText());
			addedDate = tc.getTimestampJava();
			
			if ( !name.isEmpty() && companyType != null && !address.isEmpty() && zipCode != null && !mainPhone.isEmpty() && mainService != null )
			{
				success = true;
			}
			else
			{
				setError("Inkorrekte data!");
			}
			
			if (success) {
				cc.createCompany(cvr, name, companyType, address, zipCode, mainPhone, mainMail, website, mainService, secService, noOfEmployees, addedDate);
			}
		}
		catch(NumberFormatException e)
		{
			setError("Du har ikke indtastet et postnummer!");
		}	
		
		return success;
	}

	private void setError(String err) {
		lblError.setText(err);
	}
}

